<template>
  <view class="goods_detail" v-if="goodsDetail">
    <!-- 轮播图 -->
    <u-swiper
      :list="goodsDetail.pics"
      name="pics_big"
      height="497"
      img-mode="aspectFit"
      @click="handlePreview"
    ></u-swiper>
    <!-- 商品信息模块 -->
    <view class="goods_info">
      <!-- 商品价格 -->
      <view class="goods_price">{{ goodsDetail.goods_price }}</view>
      <!-- 商品标题 -->
      <view class="goods_name">{{ goodsDetail.goods_name }}</view>
    </view>
    <!-- 商品详情图片 -->
    <view class="footer-goods-detail">/ 商品详情 /</view>
    <view v-html="goodsDetail.goods_introduce"></view>

    <!-- 底部通栏 -->
    <view class="navigation">
      <view class="left">
        <view class="item">
          <u-icon
            name="server-fill"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">客服</view>
        </view>
        <view class="item">
          <u-icon
            name="home"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">店铺</view>
        </view>
        <navigator
          class="item car"
          url="/pages/cart/cart"
          open-type="switchTab"
        >
          <u-badge
            class="car-num"
            :count="getTotal"
            type="error"
            :offset="[-3, -6]"
          ></u-badge>
          <u-icon
            name="shopping-cart"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">购物车</view>
        </navigator>
      </view>
      <view class="right">
        <view class="cart btn u-line-1" @click="handleAddCart">加入购物车</view>
        <view class="buy btn u-line-1">立即购买</view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapMutations, mapGetters } from "vuex";
export default {
  data() {
    return {
      goodsDetail: null,
    };
  },
  computed: {
    ...mapGetters("cart", ["getTotal"]),
  },
  onLoad(options) {
    // console.log(80, options);
    this.getGoodsDetail(options.goods_id);
  },
  methods: {
    // ...mapMutations("模块名",["模块中的函数1","模块中的函数2"])
    ...mapMutations("cart", ["addCart"]),
    async getGoodsDetail(goods_id) {
      const res = await this.$u.get(`/goods/detail`, { goods_id });
      this.goodsDetail = res.message;
    },
    // 点击轮播图预览
    handlePreview(index) {
      const urls = this.goodsDetail.pics.map((item) => item.pics_big);
      const current = index;
      uni.previewImage({ urls, current });
    },
    // 加入购物车事件处理函数
    handleAddCart() {
      // console.log(111);
      // 调用定义在mutations中的函数
      this.addCart(this.goodsDetail);
    },
  },
};
</script>

<style lang="scss">
.goods_detail {
  padding-bottom: 104rpx;
  .goods_info {
    .goods_price {
      color: #eb4450;
      margin-bottom: 25rpx;
      &:before {
        content: "￥";
        font-size: 20rpx;
      }
    }
    .goods_name {
      font-size: 30rpx;
      color: #434343;
      margin-bottom: 25rpx;
    }
  }
  .footer-goods-detail {
    text-align: center;
  }
}

// 底部通栏样式
.navigation {
  position: fixed;
  width: 100%;
  bottom: 0;
  right: 0;

  display: flex;
  margin-top: 100rpx;
  border: solid 2rpx #f2f2f2;
  background-color: #ffffff;
  padding: 16rpx 0;
  .left {
    display: flex;
    font-size: 20rpx;
    .item {
      margin: 0 30rpx;
      &.car {
        text-align: center;
        position: relative;
        .car-num {
          position: absolute;
          top: -10rpx;
          right: -10rpx;
        }
      }
    }
  }
  .right {
    display: flex;
    font-size: 28rpx;
    align-items: center;
    .btn {
      line-height: 66rpx;
      padding: 0 30rpx;
      border-radius: 36rpx;
      color: #ffffff;
    }
    .cart {
      background-color: #ed3f14;
      margin-right: 30rpx;
    }
    .buy {
      background-color: #ff7900;
    }
  }
}
</style>